package com.zisuyi.uilibrary.uitils

import android.app.Activity
import android.content.*
import android.content.pm.PackageManager
import android.net.Uri
import com.weilele.mvvm.app
import com.weilele.mvvm.utils.activity.toast
import com.zisuyi.uilibrary.R
import com.zisuyi.game.sdk.utils.UserData


/**
 * 分享的工具类
 */
private val packageName = app.packageName

//分享链接的包名
private val GP_SHORT_URL = "https://play.google.com/store/apps/details?id=$packageName"
private const val PACKAGE_NAME_FB = "com.facebook.katana"
private const val PACKAGE_NAME_WHATSAPP = "com.whatsapp"
private const val PACKAGE_NAME_GOOGLE_PLUS = "com.google.android.apps.plus"
private const val PACKAGE_NAME_INSTAGRAM = "com.instagram.android"
private const val SHARE_TYPE_TEXT = "text/plain"
private val SHARE_TYPE_IMAGE = "image/*"
const val SHARE_REQUEST_CODE_MORE = 1000
const val SHARE_REQUEST_CODE_FB = 1001
const val SHARE_REQUEST_CODE_INS = 1002
const val SHARE_REQUEST_CODE_GP = 1003

private val baseShareUrl: String?
    get() = UserData.userInfo?.shareLink
private val shareText = String.format(
    app.getString(R.string.share_other_text),
    app.getString(R.string.app_name),
    baseShareUrl
)

/**
 * 调用本地分享文本
 */
private fun shareText(activity: Activity, request: Int, content: String, packageName: String?) {
    try {
        copyToClipboard(content)
        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.putExtra(Intent.EXTRA_TEXT, content)//注意：这里只是分享文本内容
        sendIntent.type = SHARE_TYPE_TEXT
        packageName?.let {
            sendIntent.setPackage(packageName)
        }
        activity.startActivityForResult(sendIntent, request)
    } catch (e: Exception) {
        e.printStackTrace()
        "Share Error".toast()
    }

}

/**
 * 分享更多
 */
fun shareMore(activity: Activity?, code: String) {
    activity ?: return
    shareText(activity, SHARE_REQUEST_CODE_MORE, shareText, null)
}

/**
 * 分享到facebook
 */
fun shareFb(activity: Activity?, code: String) {
    activity ?: return
    if (isAppInstalled(activity, PACKAGE_NAME_FB)) {
        shareText(activity, SHARE_REQUEST_CODE_FB, shareText, PACKAGE_NAME_FB)
    } else {
        "Please install Facebook First!!".toast()
    }
}


/**
 * 文字复制到剪切板
 */
fun copyToClipboard(text: String) {
    //获取剪贴板管理器：
    val cm = app.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    // 创建普通字符型ClipData
    val mClipData = ClipData.newPlainText("Label", text)
    // 将ClipData内容放到系统剪贴板里。
    cm.setPrimaryClip(mClipData)
    "Copy Success".toast()
}

/**
 * 分享到ins
 */
fun shareIns(activity: Activity?, code: String) {
    activity ?: return
    if (isAppInstalled(activity, PACKAGE_NAME_INSTAGRAM)) {
        val text = shareText
        copyToClipboard(text)
        var shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND//设置分享行为
        shareIntent.type = SHARE_TYPE_IMAGE //设置分享内容的类型
        shareIntent.putExtra(Intent.EXTRA_STREAM, getImgUri(activity))
        shareIntent.setPackage(PACKAGE_NAME_INSTAGRAM)
        //创建分享的Dialog
        shareIntent =
            Intent.createChooser(shareIntent, "Share ${activity.getString(R.string.app_name)}")
        activity.startActivity(shareIntent)
    } else {
        "Please install Instagram First!!".toast()
    }
}

/**
 * 资源图片转uri
 * 由于ins分享必须要图片
 */
fun getImgUri(context: Context): Uri {
    val r = context.resources
    return Uri.parse(
        ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + r.getResourcePackageName(R.drawable.ic_launcher) + "/"
                + r.getResourceTypeName(R.drawable.ic_launcher) + "/"
                + r.getResourceEntryName(R.drawable.ic_launcher)
    )
}

/**
 * 分享到google+
 */
fun shareGp(activity: Activity?, code: String) {
    activity ?: return
    if (isAppInstalled(activity, PACKAGE_NAME_GOOGLE_PLUS)) {
        shareText(activity, SHARE_REQUEST_CODE_GP, shareText, PACKAGE_NAME_GOOGLE_PLUS)
    } else {
        "Please install Google+ First!!".toast()
    }

}

/**
 * 手机是否安装某个app
 */
fun isAppInstalled(context: Context, packageName: String): Boolean {
    synchronized(context) {
        return try {
            val pm = context.packageManager
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
            true
        } catch (e: Throwable) {
            false
        }
    }
}

